| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
use SureCart\Support\Currency; |
| 4 |
class ESHB_Core { |
| 5 |
|
| 6 |
private static $_instance = null; |
| 7 |
|
| 8 |
public static function instance() { |
| 9 |
|
| 10 |
if ( is_null( self::$_instance ) ) { |
| 11 |
self::$_instance = new self(); |
| 12 |
} |
| 13 |
return self::$_instance; |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
add_action( 'init', [$this, 'eshb_load_textdomain'] ); |
| 19 |
|
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
public function eshb_load_textdomain() { |
| 24 |
load_plugin_textdomain( |
| 25 |
'easy-hotel', |
| 26 |
false, |
| 27 |
dirname(plugin_basename(dirname(__FILE__, 2))) . '/languages' |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
public function eshb_send_html_email($to, $subject, $message, $from_name = 'reactheme.com/easyhotel', $from_email = 'rubel.reacthemes@gmail.com') { |
| 32 |
|
| 33 |
require_once( trailingslashit( ABSPATH ) .'wp-load.php' ); |
| 34 |
|
| 35 |
if (!function_exists('wp_mail')) { |
| 36 |
error_log('Error: wp_mail() function is missing!'); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
// Set content type to HTML |
| 42 |
add_filter('wp_mail_content_type', function() { return 'text/html'; }); |
| 43 |
|
| 44 |
// Set email headers |
| 45 |
$headers = array(); |
| 46 |
$headers[] = "From: {$from_name} <{$from_email}>"; |
| 47 |
$headers[] = "Reply-To: {$from_email}"; |
| 48 |
|
| 49 |
// Send email |
| 50 |
$sent = wp_mail($to, $subject, $message, $headers); |
| 51 |
|
| 52 |
// Remove filter to avoid conflicts |
| 53 |
remove_filter('wp_mail_content_type', function() { return 'text/html'; }); |
| 54 |
|
| 55 |
if (!$sent) { |
| 56 |
error_log('Email sending failed: ' . print_r(error_get_last(), true)); |
| 57 |
} |
| 58 |
|
| 59 |
//return $sent; // Returns true if sent, false if failed |
| 60 |
return $sent ? 'Email Sent!' : 'Failed to Send!'; |
| 61 |
} |
| 62 |
|
| 63 |
public function get_eshb_currency_symbol(){ |
| 64 |
|
| 65 |
$eshb_settings = get_option( 'eshb_settings' ,[]); |
| 66 |
$booking_type = isset($eshb_settings['booking-type']) ? $eshb_settings['booking-type'] : ''; |
| 67 |
|
| 68 |
if( !empty($eshb_settings['currency_symbol']) ){ |
| 69 |
$currency_symbol = $eshb_settings['currency_symbol']; |
| 70 |
}else if($booking_type == 'woocommerce' && class_exists('woocommerce')){ |
| 71 |
$currency_symbol = class_exists('woocommerce') ? get_woocommerce_currency_symbol() : '$'; |
| 72 |
}else if($booking_type == 'surecart' && class_exists('SureCart')){ |
| 73 |
$currency = new Currency(); |
| 74 |
$currency_symbol = $currency->getCurrencySymbol(); |
| 75 |
}else { |
| 76 |
$currency_symbol = '$'; |
| 77 |
} |
| 78 |
|
| 79 |
return $currency_symbol; |
| 80 |
} |
| 81 |
|
| 82 |
public function get_eshb_currency_position(){ |
| 83 |
|
| 84 |
$currency_position = 'left'; |
| 85 |
|
| 86 |
$eshb_settings = get_option( 'eshb_settings' ,[]); |
| 87 |
if(isset($eshb_settings['currency_position']) && !empty($eshb_settings['currency_position'])){ |
| 88 |
$currency_position = $eshb_settings['currency_position']; |
| 89 |
} |
| 90 |
|
| 91 |
return $currency_position; |
| 92 |
} |
| 93 |
|
| 94 |
public function get_eshb_default_start_end_date(){ |
| 95 |
$today_date = gmdate('Y-m-d'); // Get today's date |
| 96 |
|
| 97 |
// Create a DateTime object from today's date |
| 98 |
$date = new DateTime($today_date); |
| 99 |
|
| 100 |
// Add one day |
| 101 |
$date->modify('+1 day'); |
| 102 |
|
| 103 |
// Get the new date in 'Y-m-d' format |
| 104 |
$tomorrow_date = $date->format('Y-m-d'); |
| 105 |
|
| 106 |
return array( |
| 107 |
'start_date' => $today_date, |
| 108 |
'end_date' => $tomorrow_date |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
public function get_eshb_price_by_session($accomodation_id, $start_date, $end_date, $night = '', $adult = '', $children = '') { |
| 113 |
|
| 114 |
$price = ''; |
| 115 |
|
| 116 |
// Set up WP_Query arguments |
| 117 |
$qargs = array( |
| 118 |
'post_type' => 'eshb_session', |
| 119 |
'post_status' => 'publish', |
| 120 |
'meta_key' => 'eshb_session_metaboxes', |
| 121 |
'orderby' => 'ID', |
| 122 |
'order' => 'DESC', |
| 123 |
'posts_per_page' => -1, // Retrieve all posts |
| 124 |
); |
| 125 |
|
| 126 |
// Run WP_Query |
| 127 |
$query = new WP_Query($qargs); |
| 128 |
|
| 129 |
$eligible_sessions = array(); |
| 130 |
|
| 131 |
if ($query->have_posts()) { |
| 132 |
while ($query->have_posts()) { |
| 133 |
$query->the_post(); |
| 134 |
$post_id = get_the_ID(); |
| 135 |
|
| 136 |
// Get the metaboxes meta value |
| 137 |
$metaboxes = maybe_unserialize(get_post_meta($post_id, 'eshb_session_metaboxes', true)); |
| 138 |
|
| 139 |
$accomodation_ids = isset($metaboxes['accomodation_ids']) && !empty($metaboxes['accomodation_ids']) ? $metaboxes['accomodation_ids'] : []; |
| 140 |
|
| 141 |
// Check if the session is for the specified accomodation |
| 142 |
if(!empty($accomodation_ids)){ |
| 143 |
if (in_array($accomodation_id, $accomodation_ids)) { |
| 144 |
// Access the start and end dates within the metaboxes array |
| 145 |
if (isset($metaboxes['start_date']) && isset($metaboxes['end_date'])) { |
| 146 |
$session_start_date = $metaboxes['start_date']; |
| 147 |
$session_end_date = $metaboxes['end_date']; |
| 148 |
|
| 149 |
// Check if the session is within the specified date range |
| 150 |
if (($start_date <= $session_end_date) && ($start_date >= $session_start_date)) { |
| 151 |
$eligible_sessions[] = $post_id; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
}else{ |
| 156 |
// Access the start and end dates within the metaboxes array |
| 157 |
if (isset($metaboxes['start_date']) && isset($metaboxes['end_date'])) { |
| 158 |
$session_start_date = $metaboxes['start_date']; |
| 159 |
$session_end_date = $metaboxes['end_date']; |
| 160 |
|
| 161 |
// Check if the session is within the specified date range |
| 162 |
if (($start_date <= $session_end_date) && ($start_date >= $session_start_date)) { |
| 163 |
$eligible_sessions[] = $post_id; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
// If we have eligible sessions, get the price from the last session |
| 173 |
if (count($eligible_sessions) > 0) { |
| 174 |
$last_session_id = end($eligible_sessions); |
| 175 |
|
| 176 |
// Get the metaboxes for the last eligible session |
| 177 |
$last_session_metaboxes = maybe_unserialize(get_post_meta($last_session_id, 'eshb_session_metaboxes', true)); |
| 178 |
|
| 179 |
if (isset($last_session_metaboxes['session_price'])) { |
| 180 |
$price = $last_session_metaboxes['session_price']; |
| 181 |
} |
| 182 |
|
| 183 |
if(!empty($night)){ |
| 184 |
$longstay_pricing = $last_session_metaboxes['longstay_pricing']; |
| 185 |
if(!empty($longstay_pricing)){ |
| 186 |
$matched = array_filter($longstay_pricing, function($item) use ($night) { |
| 187 |
return isset($item['night']) && $item['night'] <= $night; |
| 188 |
}); |
| 189 |
|
| 190 |
$price = !empty($matched) ? array_values($matched)[0]['price'] : $price; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
$variable_pricing = $last_session_metaboxes['variable_pricing']; |
| 196 |
if(!empty($variable_pricing)){ |
| 197 |
$matched = array_filter($variable_pricing, function($item) use ($adult, $children) { |
| 198 |
if(empty($item['adult_quantity'])){ |
| 199 |
$item['adult_quantity']= 0; |
| 200 |
} |
| 201 |
|
| 202 |
if(empty($item['children_quantity'])){ |
| 203 |
$item['children_quantity']= 0; |
| 204 |
} |
| 205 |
|
| 206 |
return isset($item['adult_quantity'], $item['children_quantity']) && |
| 207 |
$item['adult_quantity'] == $adult && |
| 208 |
$item['children_quantity'] == $children; |
| 209 |
}); |
| 210 |
|
| 211 |
$price = !empty($matched) ? array_values($matched)[0]['price'] : $price; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
} |
| 216 |
} |
| 217 |
// Reset post data after query |
| 218 |
wp_reset_postdata(); |
| 219 |
wp_reset_query(); |
| 220 |
|
| 221 |
return $price; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
public function get_eshb_price($start_date = null, $end_date = null, $accomodation_id = null, $session_price = true, $night = '', $adult = '', $children = '') { |
| 226 |
|
| 227 |
if($start_date == null || empty($start_date) || $end_date == null || empty($end_date)){ |
| 228 |
$dates = $this->get_eshb_default_start_end_date(); |
| 229 |
$start_date = $dates['start_date']; |
| 230 |
$end_date = $dates['end_date']; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if( $accomodation_id == null || empty($accomodation_id) ){ |
| 235 |
global $post; |
| 236 |
$accomodation_id = $post->ID; |
| 237 |
} |
| 238 |
$price = ''; |
| 239 |
$metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true); |
| 240 |
|
| 241 |
$price = !empty($metaboxes['regular_price']) ? $metaboxes['regular_price'] : 0; |
| 242 |
|
| 243 |
if($session_price == true){ |
| 244 |
$session_price = $this->get_eshb_price_by_session($accomodation_id, $start_date, $end_date, $night, $adult, $children); |
| 245 |
$price = !empty($session_price) ? $session_price : $price; |
| 246 |
} |
| 247 |
|
| 248 |
return $price; |
| 249 |
} |
| 250 |
|
| 251 |
public function get_eshb_price_html($start_date = null, $end_date = null, $accomodation_id = null, $show_currency = true, $include_single_day_price = true, $include_day_wise_price = true, $include_session_price = true) { |
| 252 |
|
| 253 |
if($start_date == null || empty($start_date) || $end_date == null || empty($end_date)){ |
| 254 |
$dates = $this->get_eshb_default_start_end_date(); |
| 255 |
$start_date = $dates['start_date']; |
| 256 |
$end_date = $dates['end_date']; |
| 257 |
} |
| 258 |
|
| 259 |
// Get the currency symbol |
| 260 |
$currency_symbol = $this->get_eshb_currency_symbol(); |
| 261 |
|
| 262 |
// Get price values from different sources |
| 263 |
$regular_price = $this->get_eshb_price($start_date, $end_date, $accomodation_id, $include_session_price ); |
| 264 |
$day_wise_price = $this->get_eshb_day_wise_price($start_date, $end_date, $accomodation_id, false, $include_session_price); |
| 265 |
$single_day_price = $this->get_eshb_single_day_price($accomodation_id); |
| 266 |
|
| 267 |
|
| 268 |
// Determine the price to use |
| 269 |
if ($include_day_wise_price === true && !empty($day_wise_price)) { |
| 270 |
$price = $day_wise_price; |
| 271 |
} elseif ($include_single_day_price === true && !empty($single_day_price)) { |
| 272 |
$price = $single_day_price; |
| 273 |
} else { |
| 274 |
$price = $regular_price; |
| 275 |
} |
| 276 |
|
| 277 |
// Return empty string if price is empty |
| 278 |
if (empty($price)) { |
| 279 |
return ''; |
| 280 |
} |
| 281 |
|
| 282 |
// Prepare the price HTML, with or without currency symbol |
| 283 |
$currency_position = $this->get_eshb_currency_position(); |
| 284 |
$price_html = $show_currency ? $currency_symbol . $price : $price; |
| 285 |
if($currency_position == 'right') { |
| 286 |
$price_html = $show_currency ? $price . $currency_symbol : $price; |
| 287 |
} |
| 288 |
|
| 289 |
return $price_html; |
| 290 |
} |
| 291 |
|
| 292 |
public function get_eshb_single_day_price($accomodation_id){ |
| 293 |
$metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true); |
| 294 |
$single_day_price = !empty($metaboxes['single_day_price']) ? $metaboxes['single_day_price'] : 0; |
| 295 |
return $single_day_price; |
| 296 |
} |
| 297 |
|
| 298 |
public function get_eshb_day_names_from_date_ranges($start_date, $end_date){ |
| 299 |
$period = new DatePeriod( |
| 300 |
new DateTime($start_date), |
| 301 |
new DateInterval('P1D'), |
| 302 |
new DateTime($end_date) // Stop before the end date |
| 303 |
); |
| 304 |
|
| 305 |
$dayNames = []; |
| 306 |
foreach ($period as $date) { |
| 307 |
$dayNames[] = strtolower($date->format('l')); // Convert to lowercase |
| 308 |
} |
| 309 |
|
| 310 |
return $dayNames; |
| 311 |
} |
| 312 |
|
| 313 |
public function get_eshb_day_wise_price($start_date, $end_date, $accomodation_id, $fallback_regular_price = true, $session_price = true, $night = '', $adult = '', $children = ''){ |
| 314 |
|
| 315 |
if($start_date == null || empty($start_date) || $end_date == null || empty($end_date) || $accomodation_id == null || empty($accomodation_id)) return 0; |
| 316 |
|
| 317 |
|
| 318 |
$regular_price = $fallback_regular_price == true ? $this->get_eshb_price($start_date, $end_date, $accomodation_id, $session_price, $night, $adult, $children) : 0; |
| 319 |
|
| 320 |
if ($start_date === $end_date) { |
| 321 |
// Add 1 day to end date if they are the same |
| 322 |
$end_date = date('Y-m-d', strtotime($end_date . ' +1 day')); |
| 323 |
$single_day_price = $this->get_eshb_single_day_price($accomodation_id); |
| 324 |
$single_day_price = !empty($single_day_price) ? $single_day_price : $regular_price; |
| 325 |
} |
| 326 |
|
| 327 |
$period = new DatePeriod( |
| 328 |
new DateTime($start_date), |
| 329 |
new DateInterval('P1D'), |
| 330 |
new DateTime($end_date) // Stop before the end date |
| 331 |
); |
| 332 |
|
| 333 |
$dayNames = []; |
| 334 |
$days_count = 0; |
| 335 |
foreach ($period as $date) { |
| 336 |
$dayNames[] = strtolower($date->format('l')); // Convert to lowercase |
| 337 |
$days_count++; |
| 338 |
} |
| 339 |
|
| 340 |
// If only one day is selected, return the price for that day |
| 341 |
if(is_archive() || isset($_GET['eshb-search-nonce']) || isset($_GET['eshb_search_nonce'])){ |
| 342 |
$todayName = strtolower(date('l')); |
| 343 |
//remove all day names from the array but today's name |
| 344 |
$dayNames = array_filter($dayNames, function($day) use ($todayName) { |
| 345 |
return $day === $todayName; |
| 346 |
}); |
| 347 |
} |
| 348 |
|
| 349 |
$metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true); |
| 350 |
$day_wise_price = isset($metaboxes['day_wise_price']) && !empty($metaboxes['day_wise_price']) ? $metaboxes['day_wise_price'] : ''; |
| 351 |
|
| 352 |
$price_by_day = 0; |
| 353 |
|
| 354 |
if(!empty($day_wise_price) && isset($day_wise_price[0]) && !empty($day_wise_price[0])){ |
| 355 |
$day_wise_price = $day_wise_price[0]; |
| 356 |
foreach ($dayNames as $key => $day) { |
| 357 |
if( !empty($day_wise_price[$day]) ){ |
| 358 |
$price_by_day += $day_wise_price[$day]; |
| 359 |
}else{ |
| 360 |
$price_by_day += $regular_price; |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
}else{ |
| 365 |
|
| 366 |
$price_by_day = $regular_price * $days_count; |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
return $price_by_day; |
| 371 |
} |
| 372 |
|
| 373 |
public function get_eshb_avgerage_rating_count($accomodation_id){ |
| 374 |
if(class_exists('ESHB_REVIEWS')){ |
| 375 |
return (float) get_post_meta( $accomodation_id, 'eshb_avg_rating', true ); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
public function get_eshb_required_max_min_nights ($accomodation_id, $type = 'min') { |
| 380 |
$is_min_max_plugin_actvated = get_option( 'eshb_min_max_activated' ); |
| 381 |
|
| 382 |
$required_min_nights = 1; |
| 383 |
$required_max_nights = ''; |
| 384 |
|
| 385 |
if($is_min_max_plugin_actvated) { |
| 386 |
$accomodation_metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true); |
| 387 |
|
| 388 |
$is_global_source_for_min_max = isset($accomodation_metaboxes['is_global_source_for_min_max']) ? $accomodation_metaboxes['is_global_source_for_min_max'] : ''; |
| 389 |
|
| 390 |
$required_min_nights = !empty($accomodation_metaboxes['required_min_nights']) ? $accomodation_metaboxes['required_min_nights'] : 1; |
| 391 |
$required_max_nights = !empty($accomodation_metaboxes['required_max_nights']) ? $accomodation_metaboxes['required_max_nights'] : ''; |
| 392 |
|
| 393 |
if($is_global_source_for_min_max) { |
| 394 |
$eshb_min_max_settings = get_option( 'eshb_min_max_settings', []); |
| 395 |
$required_min_nights = !empty($eshb_min_max_settings['required_min_nights']) ? $eshb_min_max_settings['required_min_nights'] : 1; |
| 396 |
$required_max_nights = !empty($eshb_min_max_settings['required_max_nights']) ? $eshb_min_max_settings['required_max_nights'] : ''; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
if($type == 'min'){ |
| 401 |
return $required_min_nights; |
| 402 |
}else{ |
| 403 |
return $required_max_nights; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
} |
| 409 |
ESHB_Core::instance(); |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|